package net.vanchu.mortgagecalculator.mortgage.calculate.mix;

import android.text.TextUtils;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.common.commonView.component.WritingOptionLayout;
import net.vanchu.mortgagecalculator.common.config.LoanRate;
import net.vanchu.mortgagecalculator.common.config.LoanWay;
import net.vanchu.mortgagecalculator.common.config.TestConfigProvider;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculateResultListener;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculatorHelper;
import net.vanchu.mortgagecalculator.mortgage.calculate.LoanNumberInputValidator;
import net.vanchu.mortgagecalculator.mortgage.calculate.OnLoanWaySwitchListener;

import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Created by Granton on 2016/12/21.
 */
class CalMixLoanPresenter implements CalMixLoanContract.Presenter, WritingOptionLayout.InputStateListener {

    private CalMixLoanContract.View view;

    private MixLoanWayInputData data;

    private OnLoanWaySwitchListener onLoanWaySwitchListener;

    private CalculateResultListener calculateResultListener;

    CalMixLoanPresenter(CalMixLoanContract.View view, MixLoanWayInputData data,
                        OnLoanWaySwitchListener onLoanWaySwitchListener,
                        CalculateResultListener calculateResultListener) {
        this.view = view;
        this.data = data;
        this.onLoanWaySwitchListener = onLoanWaySwitchListener;
        this.calculateResultListener = calculateResultListener;
    }

    @Override
    public void chooseLoanWay(LoanWay loanWay) {
        if (onLoanWaySwitchListener == null) {
            return;
        }
        if (loanWay != LoanWay.wayMix) {
            onLoanWaySwitchListener.onLoanWaySwitch(loanWay);
        }
    }

    @Override
    public void preChooseLoanWay() {
        view.showLoanWays();
    }

    @Override
    public void chooseLoanYear(int year) {
        String yearExpress = String.format(Locale.getDefault(), "%d年", year);
        view.showLoanYear(yearExpress);
        boolean reset = data.getYear() != year;
        data.setYear(year);
        if (reset) {
            data.setComRate(0);
            data.setComDiscount(0);
            data.setProRate(0);
            data.setProDiscount(0);
            view.resetComLoanRates();
            view.resetProLoanRates();
        }
        checkIfInputSatisfied();
    }

    @Override
    public void chooseComLoanRates(LoanRate loanRate, float discount) {
        String loanRateExpress;
        float rate = loanRate.getValue(LoanWay.wayCommercial, data.getYear());
        if (discount == 1) {
            loanRateExpress = rate + "%";
        } else {
            DecimalFormat formatToTwoDecimal = new DecimalFormat("#.##");
            String discountStr = TestConfigProvider.extractDisCountName(
                    formatToTwoDecimal, discount
            );
            loanRateExpress = rate + "%" + " x " + discountStr;
        }
        view.showCommercialLoanRate(loanRateExpress);
        data.setComDiscount(discount);
        data.setComRate(rate);
        checkIfInputSatisfied();
    }

    @Override
    public void chooseProLoanRates(LoanRate loanRate, float discount) {
        String loanRateExpress;
        float rate = loanRate.getValue(LoanWay.wayProvidentFund, data.getYear());
        if (discount == 1) {
            loanRateExpress = rate + "%";
        } else {
            DecimalFormat formatToTwoDecimal = new DecimalFormat("#.##");
            String discountStr = TestConfigProvider.extractDisCountName(
                    formatToTwoDecimal, discount
            );
            loanRateExpress = rate + "%" + " x " + discountStr;
        }
        view.showProvidentFundRate(loanRateExpress);
        data.setProDiscount(discount);
        data.setProRate(rate);
        checkIfInputSatisfied();
    }

    @Override
    public void preChooseLoanYear() {
        view.showLoanYears();
    }

    @Override
    public void preChooseComLoanRates() {
        if (data.getYear() == 0) {
            view.tipError("请先选择贷款年限");
            return;
        }
        view.showCommercialLoanRates();
    }

    @Override
    public void preChooseProLoanRates() {
        if (data.getYear() == 0) {
            view.tipError("请先选择贷款年限");
            return;
        }
        view.showProvidentFundLoanRates();
    }

    @Override
    public void applyCalculate() {
        calculateResultListener.onMixWayCalculateResult(CalculatorHelper.getInstance().calMix(data));
    }

    @Override
    public WritingOptionLayout.InputStateListener asInputStateListener() {
        return this;
    }

    @Override
    public void onInputValid(int id, String validText) {
        float value;
        if (TextUtils.isEmpty(validText)) {
            // 当用户输入数字又清空输入框内的文字之后触发这个分支
            value = 0;
        } else {
            value = Float.parseFloat(validText);
        }
        if (id == R.id.layout_pro_loan_value) {
            data.setProValue(value);
            return;
        }
        data.setComValue(value);
        checkIfInputSatisfied();
    }

    private void checkIfInputSatisfied() {
        if (data.inputValid()) {
            view.showEnableCalButton();
        } else {
            view.showDisableCalButton();
        }
    }

    @Override
    public void onInputInvalid(String errorMsg) {
        view.tipError(errorMsg);
    }

    @Override
    public WritingOptionLayout.InputValidator asInputValidator() {
        return new LoanNumberInputValidator(new String[1]);
    }
}